##LOAD R PACKAGES
library(plyr)
library(dplyr)
library(foreign)
library(psych)
library(corrr)
library(car)
library(readstata13)

##LOAD UTIL FUNCTIONS
source('utils.R')
 
##LODING MERGED CCES/DIME DATA
load('hh_cces_all.rdata')

##LOADING HILL-HUBER FACTOR ANALYSIS
ccesfa <- read.dta13('CCES12_Common_VV_fa.dta')
ccesfa <- ccesfa[match(cces.all$V101,ccesfa$V101),]


##SUBSET ON ITEMS USED TO CONSTRUCT HILL-HUBER FACTOR SCORES
cc <- cces.all[,c('CC320','CC321','CC322_1','CC322_2','CC322_3','CC322_4','CC322_5','CC322_6',
                     'CC324','CC325','CC326','CC327','CC328','CC329'),]
rownames(cc) <- cces.all[,1]
cc <- as.matrix(cc)
cc[is.na(cc)] <- 'missing'
cc[(cc %in% c('miss'))] <- 'missing'
rownames(cc) <- cces.all[,1]
cc <- as.data.frame(cc)

##RAW ITEMS
zz <- model.matrix(~-1 + .,data=cc)

##HILL-HUBER ONE-HOT CODING SCHEME FOR CATEGORICAL ITEMS
zz.one.hot <- ccesfa[,-c(1,37,38)]

##CROHNBACH ALPHA
qq <- psych::fa(zz[complete.cases(zz),],factors=6)
psych::alpha(zz,check.keys=T)

##CROHNBACH ALPHA: ONE-HOT
qq.onehot <- psych::fa(zz.one.hot,factors=6)
psych::alpha(zz.one.hot,check.keys=T)

##INTER ITEM CORRELATION
inter_item <- zz %>% correlate(use='pairwise.complete.obs') %>% select(-rowname)
mean(abs(as.matrix(inter_item)),na.rm=T)

##INTER ITEM CORRELATION: ONE-HOT CODING 
inter_item.onehot <- zz.one.hot %>% correlate(use='pairwise.complete.obs') %>% select(-rowname) 
mean(abs(as.matrix(inter_item)),na.rm=T)


cces.all$approval <- as.numeric(cces.all$CC308a) - as.numeric(cces.all$CC308b) 
cces.all$ideo <- as.numeric(cces.all$ideo5)
gg <- cces.all[,c('ideofactor',
              'rollcallfactor',
              'ideo',
              'approval')]

xlabs <- c('Factor Scores\n(Hill and Huber 2017)',
           'Factor Scores\n(CCES Roll Call Items)',
           'Self-Reported Ideology',
           'Approval Ratings\n(President-Congress)')
pcol <- ifelse(is.na(cces.all$isdem),'orange',
               ifelse(cces.all$isdem==1,'blue',
                      ifelse(cces.all$isdem==0,'red',NA))) 

##PDF
pdf(file='figures/fig_A1_cces_scatterplot_matrix.pdf',width=8,height=8)
print( pairs(gg,
            panel=points2,
            labels=c('Policy Scores\n(Hill and Huber 2017)',
                     'Factor Scores\n(CCES Roll Call Items)',
                     'Self-Reported Ideology',
                     'Approval Ratings\n(President-Congress)'),            
            col=pcol,
            jit=TRUE,
            upper.panel = panel.cor,
            pch=20))
dev.off()

##EPS
cairo_ps(file='figures/fig_A1_cces_scatterplot_matrix.eps',width=8,height=8)
print( pairs(gg,
            panel=points2,
            labels=c('Policy Scores\n(Hill and Huber 2017)',
                     'Factor Scores\n(CCES Roll Call Items)',
                     'Self-Reported Ideology',
                     'Approval Ratings\n(President-Congress)'),            
            col=pcol,
            jit=TRUE,
            upper.panel = panel.cor,
            pch=20))
dev.off()


